<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>WASM Web 示例</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }

      body {
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        min-height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 20px;
      }

      .container {
        background: white;
        border-radius: 12px;
        box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
        max-width: 600px;
        width: 100%;
        padding: 40px;
      }

      h1 {
        color: #333;
        margin-bottom: 10px;
        font-size: 28px;
      }

      .subtitle {
        color: #666;
        margin-bottom: 30px;
        font-size: 14px;
      }

      .input-group {
        display: flex;
        gap: 10px;
        margin-bottom: 15px;
      }

      input {
        flex: 1;
        padding: 12px;
        border: 2px solid #e0e0e0;
        border-radius: 6px;
        font-size: 16px;
        transition: border-color 0.3s;
      }

      input:focus {
        outline: none;
        border-color: #667eea;
      }

      button {
        padding: 12px 24px;
        font-size: 16px;
        font-weight: 600;
        cursor: pointer;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border: none;
        border-radius: 6px;
        transition: transform 0.2s, box-shadow 0.2s;
      }

      button:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 16px rgba(102, 126, 234, 0.4);
      }

      button:active {
        transform: translateY(0);
      }

      #result {
        margin-top: 20px;
        padding: 20px;
        background: #f8f9fa;
        border-radius: 8px;
        min-height: 80px;
        display: none;
      }

      #result.show {
        display: block;
      }

      .result-item {
        padding: 10px 0;
        font-size: 18px;
        color: #333;
        border-bottom: 1px solid #e0e0e0;
      }

      .result-item:last-child {
        border-bottom: none;
      }

      .result-item strong {
        color: #667eea;
      }

      .success {
        color: #28a745;
      }

      .error {
        color: #dc3545;
        padding: 15px;
        background: #f8d7da;
        border-radius: 6px;
      }

      .loading {
        text-align: center;
        color: #666;
      }

      .info {
        margin-top: 15px;
        padding: 12px;
        background: #e7f3ff;
        border-left: 4px solid #667eea;
        border-radius: 4px;
        font-size: 14px;
        color: #666;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <h1>🧮 WASM 计算器</h1>
      <p class="subtitle">WebAssembly + TypeScript 示例</p>

      <div class="input-group">
        <input type="number" id="num1" placeholder="第一个数" value="10" />
        <input type="number" id="num2" placeholder="第二个数" value="20" />
      </div>

      <button id="addBtn">➕ 加法</button>
      <button id="multiplyBtn">✖️ 乘法</button>

      <div id="result"></div>

      <div class="info">💡 这个示例使用 WebAssembly 执行计算，C 代码编译为 WASM</div>
    </div>

    <script type="module">
      import { createCalculator } from "./dist/js/index.js";

      // 创建计算器实例（自动预加载 WASM）
      const calculator = await createCalculator("./dist/wasm/add.wasm");
      const resultDiv = document.getElementById("result");
      console.log("✅ 计算器初始化完成");

      // 获取输入值
      function getInputs() {
        const num1 = parseFloat(document.getElementById("num1").value) || 0;
        const num2 = parseFloat(document.getElementById("num2").value) || 0;
        return { num1, num2 };
      }

      // 显示结果
      function showResult(operation, num1, num2, result) {
        resultDiv.innerHTML = `
          <div class="result-item">
            <strong>运算:</strong> ${operation}
          </div>
          <div class="result-item">
            <strong>输入:</strong> ${num1} 和 ${num2}
          </div>
          <div class="result-item success">
            <strong>结果:</strong> ${result}
          </div>
        `;
        resultDiv.className = "show";
      }

      // 显示错误
      function showError(message) {
        resultDiv.innerHTML = `<div class="error">❌ ${message}</div>`;
        resultDiv.className = "show";
      }

      // 显示加载中
      function showLoading() {
        resultDiv.innerHTML = '<div class="loading">⏳ 计算中...</div>';
        resultDiv.className = "show";
      }

      // 加法
      document.getElementById("addBtn").addEventListener("click", async () => {
        showLoading();
        try {
          const { num1, num2 } = getInputs();
          const result = await calculator.add(num1, num2);
          showResult("加法 (add)", num1, num2, result);
        } catch (err) {
          showError(err.message);
        }
      });

      // 乘法
      document.getElementById("multiplyBtn").addEventListener("click", async () => {
        showLoading();
        try {
          const { num1, num2 } = getInputs();
          const result = await calculator.multiply(num1, num2);
          showResult("乘法 (multiply)", num1, num2, result);
        } catch (err) {
          showError(err.message);
        }
      });
    </script>
  </body>
</html>
